<template>
	<view class="">
		<chat id="chat"  :quoteInfo="quoteInfo" :enquiryInfo="enquiryInfo" :username="username" ref="chat" chatType="singleChat" @onClickInviteMsg="onClickMsg"></chat>
	</view>
</template>

<script>
	let disp = require("../../utils/broadcast");
	let msgStorage = require("@/components/chat/msgstorage");
	let WebIM = require("@/utils/WebIM")["default"];
	let msgType = require("@/components/chat/msgtype");
	import Custom from "@/components/chat/inputbar/suit/custom/custom"; //引入自定义消息组件
	import chat from "../../components/chat/chat.vue";
	import {
		quoteDetails,
		enquiryDetails
	} from '@/api/enquriy/index.js'
	export default {
		data() {
			return {
				username: {
					your: ""
				},

				// 报价信息
				quoteInfo: {
					id: ''
				},
				// 询价信息
				enquiryInfo: {
					id: ''
				},
				quoteId: '',
				unitObj: {
					'0': '米',
					'1': '条',
					'2': '码',
					'3': '公斤',
					'4': '本',
					'5': '包',
					'6': '粒',
					'7': '对',
					'8': '斤',
					'9': '扎',
					'10': '个',
					'11': '屡',
					'12': '张',
					'13': '套',
					'14': '板/个'
				},
			};
		},

		components: {
			chat,
			Custom
		},
		props: {},
		// options = 系统传入的 url 参数
		onLoad(options) {
			let username = JSON.parse(decodeURIComponent(options.username));
		
			// let username = JSON.parse(options.username);
			this.setData({
				username: username
			});
			
			// console.log('这是对话信息', username);
			// this.toId = username.your
			// this.myName = username.myName
			// // this.goodsInfo = JSON.parse(decodeURIComponent(options.goodsInfo))
			
			
			// 生成的支付宝小程序在onLoad里获取不到，这里放到全局变量下
			uni.username = username;
			
			uni.setNavigationBarTitle({
				title: username.yourNickName
			});


			let quoteId = uni.getStorageSync('quoteId')
			let enquiryId = uni.getStorageSync('enquiryId')

			console.log('enquiryId~~~~~', enquiryId);
			if (quoteId) {
				this.getQuoteDetails(quoteId)
			}

			if (enquiryId) {
				this.getEnquiryDetails(enquiryId)
			}

		},

		onUnload() {
			disp.fire("em.chatroom.leave");
			uni.removeStorageSync('quoteId')
			uni.removeStorageSync('enquiryId')
		},

		onPullDownRefresh: function() {
			uni.showNavigationBarLoading();
			this.$refs.chat.getMore();
			// 停止下拉动作
			uni.hideNavigationBarLoading();
			uni.stopPullDownRefresh();
		},
		methods: {
			// 取报价详情
			async getQuoteDetails(id) {
				console.log('进来了');
				let res = await quoteDetails({
					quoteId:id
				})
				console.log('这是报价单详情', res);
				this.quoteInfo = res.data
			},
			// 取询价详情
			async getEnquiryDetails(id) {
				let res = await enquiryDetails({
					enquiryId: id
				})
				console.log('这是询价单详情', res);
				res.data.enquiryPicture = JSON.parse(res.data.enquiryPicture)
				this.enquiryInfo = res.data
			},

			//测试发送自定义消息
			sendCustomMsg() {
				this.$refs.chatCustom.sendCustom();
			},
			goDetail() {
				uni.navigateTo({
					url: `/pages/opportunity/quote-detail?id=${this.quoteInfo.id}`
				})
			},
			goEnquiryDetail() {
				uni.navigateTo({
					url: `/pages/opportunity/enquiry-detail?enquiryId=${this.enquiryInfo.enquiryId}`
				})
			},
			onClickMsg(msg) {
				msg.action = 'join'
				uni.navigateTo({
					url: "../emedia/index?srcData=" + JSON.stringify(msg)
				});
			}
		}
	};
</script>
<style>
	@import "./chatroom.css";
</style>
<style lang="scss" scoped>
	.goods-box {
		position: fixed;
		z-index: 99;
		top: 0;
		width: 100%;
	}
</style>